import http from './http'

// 笔记相关类型定义
export interface Note {
  id: number
  userId: number
  courseId: number
  lessonId?: number
  videoTimestamp?: number
  title: string
  content: string
  tags?: string[]
  noteType: 'GENERAL' | 'VIDEO_TIMESTAMP' | 'CHAPTER_SUMMARY' | 'QUESTION'
  isPublic: boolean
  createdAt: string
  updatedAt: string
  courseTitle?: string
  lessonTitle?: string
  formattedTimestamp?: string
}

export interface NoteRequest {
  courseId: number | null
  lessonId?: number
  videoTimestamp?: number
  title: string
  content: string
  tags?: string[]
  noteType?: 'GENERAL' | 'VIDEO_TIMESTAMP' | 'CHAPTER_SUMMARY' | 'QUESTION'
  isPublic?: boolean
}

export interface NotesPageResponse {
  content: Note[]
  totalElements: number
  totalPages: number
  size: number
  number: number
}

// 笔记API
export const notesApi = {
  // 创建笔记
  createNote(request: NoteRequest): Promise<Note> {
    return http.post('/v1/notes', request)
  },

  // 更新笔记
  updateNote(noteId: number, request: NoteRequest): Promise<Note> {
    return http.put(`/v1/notes/${noteId}`, request)
  },

  // 删除笔记
  deleteNote(noteId: number): Promise<void> {
    return http.delete(`/v1/notes/${noteId}`)
  },

  // 获取笔记详情
  getNoteById(noteId: number): Promise<Note> {
    return http.get(`/v1/notes/${noteId}`)
  },

  // 获取我的所有笔记
  getMyNotes(page = 0, size = 20, sortBy = 'created'): Promise<NotesPageResponse> {
    return http.get('/v1/notes/my', {
      params: { page, size, sortBy }
    })
  },

  // 获取课程笔记
  getCourseNotes(courseId: number, page = 0, size = 20): Promise<NotesPageResponse> {
    return http.get(`/v1/notes/course/${courseId}`, {
      params: { page, size }
    })
  },

  // 获取章节笔记
  getLessonNotes(lessonId: number, courseId: number): Promise<Note[]> {
    return http.get(`/v1/notes/lesson/${lessonId}`, {
      params: { courseId }
    })
  },

  // 获取视频时间点笔记
  getVideoTimestampNotes(lessonId: number): Promise<Note[]> {
    return http.get(`/v1/notes/video-timestamps/${lessonId}`)
  },

  // 搜索笔记
  searchNotes(keyword: string, page = 0, size = 20): Promise<NotesPageResponse> {
    return http.get('/v1/notes/search', {
      params: { keyword, page, size }
    })
  },

  // 根据标签获取笔记
  getNotesByTag(tag: string): Promise<Note[]> {
    return http.get(`/v1/notes/tag/${tag}`)
  },

  // 获取常用标签
  getPopularTags(limit = 10): Promise<string[]> {
    return http.get('/v1/notes/tags/popular', {
      params: { limit }
    })
  }
}
